package 集合.MyList;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class List存储学生对象并遍历 {
    public static void main(String[] args) {
        //创建collection集合对象
        List<Student> list=new ArrayList<>() {};
        //创建学生对象
        Student s1=new Student("林青霞",30);
        Student s2=new Student("张曼玉",30);
        Student s3=new Student("王祖贤",30);
        //把学生添加到集合
        list.add(s1);
        list.add(s2);
        list.add(s3);
        //遍历
        //迭代器
        Iterator<Student> it=list.iterator();
        while (it.hasNext()){
            Student s=it.next();
            System.out.println(s.getName()+s.getAge());
        }
        //索引
        for (int i=0;i<list.size();i++){
            Student s=list.get(i);
            System.out.println(s.getName()+s.getAge());
        }

    }
}
